

# Create codebook function ------------------------------------------------

make_ODK_codebook <- function(choices_sheet,questions_sheet,remove_non_questions = TRUE){
  choices <- choices_sheet
  Qs <- questions_sheet
  
  codebook <- lapply(
    X = Qs$name,
    FUN = function(qname){
      type <- Qs$type[which(Qs$name == qname)][1]
      is_select <- grepl(pattern = "select",x = type,ignore.case = T)
      
      if(is_select){
        list_name <- unlist(strsplit(x = as.character(type),split = "\\s+"))[2]
        matches <- grep(pattern = paste0("\\b",list_name,"\\b"),x = choices$list_name,ignore.case = T)
        
        if(!"label.english" %in% names(choices)){
          choices$label.english <- choices$label
        }
        
        if(!"name" %in% names(choices)){
          choices$name <- choices$value
        }
        
        return_labels <- data.frame(
          integer = choices$name[matches],
          label = choices$label.english[matches]
        )
        
      } else {
        return_labels <- type
      }
      
      if(!"label.english" %in% names(Qs)){
        Qs$label.english <- Qs$label
      }
      
      question <- Qs$label.english[which(Qs$name == qname)][1]
      
      return_labels <- list(question = question, answer = return_labels)
      
      return(return_labels)
    }
  )
  
  names(codebook) <- Qs$name
  
  if(remove_non_questions){
    is_not_question <- unlist(sapply(
      X = codebook,
      FUN = function(x){
        (!is.data.frame(x$answer)) & grepl(pattern = "note|group|geopoint|calculate|audio|phonenumber|start|end",x = x$answer,ignore.case = TRUE)[1]
      }))
    
    codebook <- codebook[-which(is_not_question,arr.ind = T)]
    
  }
  
  return(codebook)
  
}

# Uganda round 1 codebook ------------------------------------------------

ug1cb <- make_ODK_codebook(
  choices_sheet = read.csv("02_codebooks/uganda1_choices.csv"),
  questions_sheet = read.csv("02_codebooks/uganda1_Qs.csv"),
  remove_non_questions = FALSE)

# Uganda round 2 codebook ------------------------------------------------

ug2cb <- make_ODK_codebook(
  choices_sheet = read.csv("02_codebooks/uganda2_choices.csv", stringsAsFactors = F),
  questions_sheet = read.csv("02_codebooks/uganda2_Qs.csv", stringsAsFactors = F),
  remove_non_questions = FALSE
)

# Uganda round 3 codebook ------------------------------------------------

ug3cb <- make_ODK_codebook(
  choices_sheet = read.csv("02_codebooks/uganda3_choices.csv", stringsAsFactors = F),
  questions_sheet = read.csv("02_codebooks/uganda3_Qs.csv", stringsAsFactors = F),
  remove_non_questions = FALSE
)


# Tanzania round 2 codebook -----------------------------------------------

tan2cb <-  make_ODK_codebook(
  choices_sheet = read.csv("02_codebooks/tanzania2_choices.csv", stringsAsFactors = F),
  questions_sheet = read.csv("02_codebooks/tanzania2_questions.csv", stringsAsFactors = F),
  remove_non_questions = FALSE
)


# Tanzania round 3 codebook -----------------------------------------------


tan3cb <-  make_ODK_codebook(
  choices_sheet = read.csv("02_codebooks/tanzania3_choices.csv", stringsAsFactors = F),
  questions_sheet = read.csv("02_codebooks/tanzania3_questions.csv", stringsAsFactors = F),
  remove_non_questions = FALSE
)


# Handy function for variable names ---------------------------------------

texttt <- function(x){
  gsub(pattern = "_",replacement = "\\\\_",x = paste0('{\\texttt{', x, '}}'))
}





